package data.email;

import data.Document;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * EmailDocument adalah representasi file email
 *
 * @author Ali Suliman Abubaker Naser
 */
public class EmailDocument extends Document {

    /**
     * jumlah karakter maksimal dalam satu line
     */
    private static Integer max_char_line = 2000;
    /**
     * jumlah line maksimal dalam satu email
     */
    private static Integer max_line_count = 250;

    /**
     * konstruktor dengan parameter file path ke file email file path menjadi ID
     * dari dokumen
     *
     * @param filePath file path ke email
     */
    public EmailDocument(String filePath) {
        this.setID(filePath);
        this.setAddress(filePath);
    }

    @Override
    public String toString() {
        return this.getID();
    }

    @Override
    public ArrayList<String> extractContent() {
        ArrayList<String> result = new ArrayList<String>();
        Integer lineCounter = 0;
        Scanner in;
        try {
            in = new Scanner(new File(this.getAddress()));
            result.add(in.nextLine());
            while (in.hasNext() && (lineCounter < max_line_count)) {
                String addedString = in.nextLine();
                if (addedString.length() > max_char_line) {
                    addedString = addedString.substring(0, max_char_line);
                }
                result.add(normalizeString(addedString));
                lineCounter++;
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(EmailDocument.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    /**
     * menormalkan string dengan menghapus tanda baca
     *
     * @param string string input
     * @return string yang telah dihapus dari tanda baca
     */
    private String normalizeString(String string) {
        int i;
        StringBuilder builder = new StringBuilder();
        char[] charArr = string.toLowerCase().toCharArray();
        for (i = 0; (i < charArr.length); i++) {
            if (charArr[i] == '-' || charArr[i] == '=' || charArr[i] == '`' || charArr[i] == '\'' || charArr[i] == '$' || charArr[i] == '=' || charArr[i] == '#') {
            } else {
                switch (charArr[i]) {
                    case '<': {
                        i++;
                        if (i == charArr.length) {
                        } else {
                            while ((i < charArr.length) && (charArr[i] != '>')) {
                                i++;
                            }
                        }
                        break;
                    }
                    case '\\': {
                        builder.append(' ');
                        break;
                    }
                    case '/': {
                        builder.append(' ');
                        break;
                    }
                    case '_': {
                        builder.append(' ');
                        break;
                    }
                    case '+': {
                        builder.append(' ');
                        break;
                    }
                    case '*': {
                        builder.append(' ');
                        break;
                    }
                    case '.': {
                        builder.append(' ');
                        break;
                    }
                    case '@': {
                        builder.append(' ');
                        break;
                    }
                    case '!': {
                        builder.append(' ');
                        break;
                    }
                    case '?': {
                        builder.append(' ');
                        break;
                    }
                    case ',': {
                        builder.append(' ');
                        break;
                    }
                    case ';': {
                        builder.append(' ');
                        break;
                    }
                    case '(': {
                        builder.append(' ');
                        break;
                    }
                    case ')': {
                        builder.append(' ');
                        break;
                    }
                    case '{': {
                        builder.append(' ');
                        break;
                    }
                    case '}': {
                        builder.append(' ');
                        break;
                    }
                    case '[': {
                        builder.append(' ');
                        break;
                    }
                    case ']': {
                        builder.append(' ');
                        break;
                    }
                    case '|': {
                        builder.append(' ');
                        break;
                    }
                    case '"': {
                        builder.append(' ');
                        break;
                    }
                    default: {
                        builder.append(charArr[i]);
                    }
                }
            }
        }
        return builder.toString();
    }
}
